/* 
 *  This file is part of hmvcBlog-services.
 *
 *  hmvcBlog-services is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-services is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-services.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.services.blog.entry.comment;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.googlecode.hmvc4gwt.example.hmvcblog.server.domain.blog.BlogEntry;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.domain.blog.BlogEntryComment;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.blog.entry.IBlogEntryRepository;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.blog.entry.comment.IBlogEntryCommentRepository;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryIdDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.BlogEntryCommentDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.BlogEntryCommentEditDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.BlogEntryCommentNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.BlogEntryCommentRemoveDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.exceptions.BlogEntryCommentAuthorNameEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.exceptions.BlogEntryCommentNotFoundException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.exceptions.BlogEntryCommentTextEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.exceptions.BlogEntryNotFoundException;

/**
 * 
 * 
 * @author jens.meiss
 */
@Transactional
public class BlogEntryCommentService implements IBlogEntryCommentService {

	private IBlogEntryCommentRepository blogEntryCommentRepository;

	private IBlogEntryRepository blogEntryRepository;

	public void addBlogEntryComment(BlogEntryCommentNewDTO commentNewDTO)
			throws BlogEntryCommentTextEmptyException,
			BlogEntryCommentAuthorNameEmptyException {

		if ((commentNewDTO.getAuthor() == null)
				|| (commentNewDTO.getAuthor().equals(""))) {
			throw new BlogEntryCommentAuthorNameEmptyException();
		}

		if ((commentNewDTO.getText() == null)
				|| (commentNewDTO.getText().equals(""))) {
			throw new BlogEntryCommentTextEmptyException();
		}

		BlogEntry blogEntry = getBlogEntryRepository().findById(
				commentNewDTO.getIdBlogEntry());

		BlogEntryComment blogEntryComment = new BlogEntryComment();
		blogEntryComment.setAuthorName(commentNewDTO.getAuthor());
		blogEntryComment.setBlogEntry(blogEntry);
		blogEntryComment.setDate(new Date());
		blogEntryComment.setText(commentNewDTO.getText());

		getBlogEntryCommentRepository().addBlogEntryComment(blogEntryComment);
	}

	public void editBlogEntryComment(BlogEntryCommentEditDTO commentEditDTO)
			throws BlogEntryCommentTextEmptyException,
			BlogEntryCommentAuthorNameEmptyException, BlogEntryNotFoundException, BlogEntryCommentNotFoundException {

		if (commentEditDTO.getAuthorName() == null
				|| (commentEditDTO.getAuthorName().equals(""))) {
			throw new BlogEntryCommentAuthorNameEmptyException();
		}

		if ((commentEditDTO.getText() == null)
				|| (commentEditDTO.getText().equals(""))) {
			throw new BlogEntryCommentTextEmptyException();
		}

		BlogEntry blogEntry = null;
		try {
			blogEntry = getBlogEntryRepository().findById(
					commentEditDTO.getIdBlogEntry());
		} catch (Exception e) {
			throw new BlogEntryNotFoundException();
		}

		BlogEntryComment blogEntryComment = null;
		try {
			blogEntryComment = getBlogEntryCommentRepository().findById(commentEditDTO.getIdBlogEntryComment());
		} catch (Exception e) {
			throw new BlogEntryCommentNotFoundException();
		}

		if (blogEntry != blogEntryComment.getBlogEntry()) {

		}
		
		blogEntryComment.setAuthorName(commentEditDTO.getAuthorName());
		blogEntryComment.setText(commentEditDTO.getText());

		getBlogEntryCommentRepository().addBlogEntryComment(blogEntryComment);
	}

	/**
	 * @return the blogCommentRepository
	 */
	public IBlogEntryCommentRepository getBlogEntryCommentRepository() {
		return blogEntryCommentRepository;
	}

	/**
	 * @return the blogEntryRepository
	 */
	public IBlogEntryRepository getBlogEntryRepository() {
		return blogEntryRepository;
	}

	public List<BlogEntryCommentDTO> getListBlogEntryCommentDTO(
			BlogEntryIdDTO blogEntryIdDTO) {
		
		BlogEntry blogEntry = getBlogEntryRepository().findById(blogEntryIdDTO.getId());
		
		List<BlogEntryComment> listBlogEntryComment = blogEntry.getBlogComment();
		
		List<BlogEntryCommentDTO> listBlogEntryCommentDTO = new LinkedList<BlogEntryCommentDTO>();
		for (BlogEntryComment blogEntryComment : listBlogEntryComment) {
			
			BlogEntryCommentDTO blogenEntryCommentDTO = new BlogEntryCommentDTO();
			blogenEntryCommentDTO.setAuthorName(blogEntryComment.getAuthorName());
			blogenEntryCommentDTO.setText(blogEntryComment.getText());
			blogenEntryCommentDTO.setDate(blogEntryComment.getDate());
			
			listBlogEntryCommentDTO.add(blogenEntryCommentDTO);
		}
		
		return listBlogEntryCommentDTO;
	}

	public void removeBlogEntryComment(
			BlogEntryCommentRemoveDTO commentRemoveDTO) {
		
		BlogEntryComment blogEntryComment = getBlogEntryCommentRepository().findById(commentRemoveDTO.getIdBlogEntryComment());		
		getBlogEntryCommentRepository().removeBlogEntryComment(blogEntryComment);
		
	}

	/**
	 * @param blogCommentRepository
	 *            the blogCommentRepository to set
	 */
	public void setBlogEntryCommentRepository(
			IBlogEntryCommentRepository blogCommentRepository) {
		this.blogEntryCommentRepository = blogCommentRepository;
	}

	/**
	 * @param blogEntryRepository
	 *            the blogEntryRepository to set
	 */
	public void setBlogEntryRepository(IBlogEntryRepository blogEntryRepository) {
		this.blogEntryRepository = blogEntryRepository;
	}

}
